package com.zhuoyian.dl.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

public class ConnUtil {

	public static HttpURLConnection connect(URL url) {
		HttpURLConnection conn = null;
		try {
			conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5 * 1000);
			conn.setRequestMethod("GET");
//			conn.setRequestProperty(
//					"Accept",
//					"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
//			conn.setRequestProperty("Accept-Language", "zh-CN");
			conn.setRequestProperty("Referer", url.toString());
			conn.setRequestProperty("Charset", "UTF-8");
//			conn.setRequestProperty(
//					"User-Agent",
//					"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.connect();
			if (conn != null && conn.getResponseCode() == 200) {
				return conn;
			} else {
				if (conn != null) {
					conn.disconnect();
					conn = null;
				}
			}
		} catch (ProtocolException e) {
			e.printStackTrace();
			if (conn != null) {
				conn.disconnect();
				conn = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			if (conn != null) {
				conn.disconnect();
				conn = null;
			}
		}
		return null;
	}
	public static HttpURLConnection connect(URL url,int startSize,int endSize) {
		HttpURLConnection http = null;
		try {
			http = (HttpURLConnection) url
					.openConnection();
			http.setConnectTimeout(5 * 1000);
			http.setRequestMethod("GET");
//			http.setRequestProperty(
//					"Accept",
//					"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
//			http.setRequestProperty("Accept-Language", "zh-CN");
			http.setRequestProperty("Referer", url.toString());
			http.setRequestProperty("Charset", "UTF-8");
			http.setRequestProperty("Range", "bytes=" + startSize + "-"
					+ endSize);// ���û�ȡʵ�����ݵķ�Χ
//			http.setRequestProperty(
//					"User-Agent",
//					"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
			http.setRequestProperty("Connection", "Keep-Alive");
			return http;
		} catch (ProtocolException e) {
			e.printStackTrace();
			if (http != null) {
				http.disconnect();
				http = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
			if (http != null) {
				http.disconnect();
				http = null;
			}
		}
		return null;
	}

}
